var cm=multiCheckModel_create();
/** 显示添加用户面板 */

function showAddUserPanel(){
	try {
		var win = new Ext.Window(addUserWindowConfig);
		var form = new Ext.form.FormPanel(userInfoFormConfig)
		win.add(form);
		win.show();
		//form.render(win.body);
	}catch(e){
		log.error(e);
	}
}

    /**初始化该Grid; */
 function initFormData(from,rd) {
    var con = new connection({
					url : 'findUserByUserId.action',
					params : {
						json :rd.get("userId")
					},
					success : function( rep,opt) {
							if (rep.success) {
								var el=from.getEl().dom;
								var inputs = el.getElementsByTagName("input");
								Ext.each(inputs, function(el) {
									//用户名时置灰;
										if(el.name=='userName'){
											Ext.getCmp(el.id).setReadOnly(true);
											Ext.getCmp(el.id).setDisabled(true);
										}
										if(rep.obj.parentAgentId<1){
											rep.obj.parentAgentId=""
										}
										if(rep.obj.parentDealerId<1){
											rep.obj.parentDealerId=""
										}
										Ext.getCmp(el.id).setValue(rep.obj[el.name]);
									})
							} else {
								Ext.Msg.alert('错误 ', "用户(" +rd.get('userName') + ")加载失败!");
							}
					}
				})
		con.submit();
	}

var gridColumns=[{
    hidden: true,
    resizable: true,
    dataIndex: "userId"
}, {
    hidden: true,
    resizable: true,
    dataIndex: "userType"
}, {
    header: "用户名",
    align : 'center',
    sortable: true,
    resizable: true,
    dataIndex: "userName",
    width: 100
}, {
    header: "用户类型",
    align : 'center',
    sortable: true,
    resizable: true,
    dataIndex: "userTypeName",
    width: 100
}, {
    header: "用户类型",
    align : 'center',
    sortable: true,
    resizable: true,
	hideable:false,
	hidden:true,
    dataIndex: "userType",
    width: 100
},  {
    header: "公司名",
    align : 'center',
    sortable: true,
    resizable: true,
    dataIndex: "cmpName",
    width: 100
},  {
    header: "联系人",
    align : 'center',
    sortable: true,
    resizable: true,
    dataIndex: "contact",
    width: 100
},  {
    header: "所属区域",
    align : 'center',
    sortable: true,
    resizable: true,
    dataIndex: "area",
    width: 100
},  {
    header: "注册时间",
    align : 'center',
    sortable: true,
    resizable: true,
    dataIndex: "registerDate",
    width: 150
}, {
    header: "操作",
    align : 'center',
    resizable: true,
	hideable :false,
	renderer:function(value, metaData, record, rowIndex, colIndex, store){
		if (record.get('userType') == 'A') {
			return '修改';
		} else {
			return '<span style="cursor:pointer;color:blue;">修改</span>';
		}
	}
}]

function getFieldsFromColumns(colums){
	var fs=new Array();
	Ext.each(colums,function(col){
		fs.push(col.dataIndex);
	})
	return fs;
}

gridColumns.unshift(cm);
gridColumns.unshift(new Ext.grid.RowNumberer({dataIndex: "rowNumber"}));

var gridStore = new Ext.data.JsonStore({
    proxy: new Ext.data.HttpProxy({
        url: 'queryUser.action',
        failure: function(){
            Ext.MessageBox.alert("错误", "数据源获取失败!");
        }
    }),
    totalProperty: 'obj.totalSize',
    successProperty: 'success',
    root: 'obj.returnBolist',
    messageProperty: 'msg',
    fields: getFieldsFromColumns(gridColumns),
	listeners:{
		load:function(store){
		}
	}   
});


	var gridModel = new Ext.grid.ColumnModel({
			defaultSortable : true,
			defaultWidth : 85,
			columns : gridColumns
		})
	var topbar = [{
			icon : 'back/extJs/images/system/adduser.gif',
			xtype : 'button',
			text : "添加用户",
			handler : showAddUserPanel
		}];
		
		
    var userGrid = new Ext.grid.GridPanel({
			store : gridStore,
			cm : gridModel,
			region : 'center',
//			fbar : gridFootBt,
			split : true,
			sm:cm,
			loadMask : true,
			border : false,
			layout : 'fit',
			frame : true,
			bbar : new Ext.PagingToolbar({
				pageSize : 100,
				store : gridStore,
				displayInfo:true,
				displyMsg : '显示第{0}条到第{1}条记录，一共{2}条',
				emptyMsg : "没有记录",
				items: [{
		            text: '删除',
		            style :'float:left',
		            handler:function(bt,e){
		            	var grid = bt.ownerCt.ownerCt;
						var cm = grid.getSelectionModel();
						var rds=cm.getSelections();
		            	Ext.Msg.confirm("提示",'你确信要执行删除操作？' ,function(at,mg){
		            		if(at=='yes'){
								var userStr=null;
								var isAdmin = "false";
								Ext.each(rds,function(rd){
									if (rd.get('userType') == 'A') {
										isAdmin = "true";										
									} else {
										userStr=(userStr!=null)?(userStr+";"+rd.get('userId')):rd.get('userId');
									}
								});
								if (isAdmin == "true") {
									Ext.Msg.alert('提示','您删除的用户中包含超级管理员admin，超级管理员不能进行删除操作！');
									return false;
								}
								var con = new connection({
		        					url: 'doDelete.action',
		        					params: {
		            					json: userStr
		        					},
		        					success: function(result, response){
										searchUserInfo();
		            					var alt=result.success?'成功':'失败';
										Ext.Msg.alert(alt, result.msg);
		        					},
		        					failure : function(result, response) {
										searchUserInfo();
		        						var alt=result.success?'成功':'失败';
		        						Ext.Msg.alert(alt, result.msg);
										
		        					}
		    					});
		    					con.submit();
		            		}
		            	});
		            }
		        }]
			}),
			listeners : {
				'cellclick' : function cellClick(grid, row, col, e) {
					var cm = grid.getColumnModel();
					var header = cm.getColumnHeader(col);
					if (header == '操作') {
						var record = grid.getStore().getAt(row);
						if (record.get('userType') == 'A') {
							Ext.Msg.alert('提示','该用户为超级管理员，不能进行修改操作！');
						} else {
							var win=new Ext.Window(updateUserWindowConfig);
							var form=new Ext.form.FormPanel(userInfoFormConfig)
							win.add(form);
							win.show();
							initFormData(form,record);
							changeUserType(form,record.get('userType')); 
						}
					}
				}
			}
		})

      
var detailPanel=new Ext.FormPanel({
	layout:'column',
	frame:true,
	split: true,
	region: "north",
	height:100,
	id:'searchUserInfoPanel',
	regetion:'north',
	defaults:{
		columnWidth:0.16,
		layout:'form',
		labelWidth:50,
		defaults:{
			anchor: '88%'
		}
	},
	items:[{
		items:[{
			xtype:'textfield',
			fieldLabel:'&nbsp;用户名',
			name:'userName',
			id:'userName_sea_fl'
		}]
	},{
		items:[{
			xtype:'textfield',
			fieldLabel:'&nbsp;公司名',
			name:'cmpName',
			id:'cmpName_sea_fl'
		}]
	},{	
		labelWidth:60,
		columnWidth:0.18,
		items:[{
			anchor: '95%',
			xtype:'datefield',
			fieldLabel:'&nbsp;注册时间',
			format : 'Y-m-d',
			name:'registerDateFrom',
			id:'registerDateFrom_sea_fl'
		}]
	},{	
		labelWidth:20,
		columnWidth:0.14,
		items:[{
			xtype:'datefield',
			anchor: '95%',
			fieldLabel : '至 ',
			format : 'Y-m-d',
			name:'registerDateTo',
			id:'registerDateTo_sea_fl'
		}]
	},{
		items:[{
			xtype:'textfield',
			fieldLabel:'&nbsp;联系人',
			name:'contact',
			id:'contact_sea_fl'
		}]
	},{
		items:[{
			xtype:'button',
			text:'查询',
			id:'searchBt',
			anchor: '40%',
			handler: function(){
				
				var data={};
				var el=detailPanel.getEl().dom;
				var inputs = el.getElementsByTagName("input");
				Ext.each(inputs, function(p) {
					var cmp=Ext.getCmp(p.id);
					data[p.name]=cmp.getValue();
				});
				searchUserInfo(data);
			}
		}]
	}]
})
    Ext.onReady(function(){
        new Ext.Viewport({
			layout: 'fit',
			items: [{
				xtype: 'panel',
				tbar: topbar,
				layout: 'border',
				items: [{
					region: 'north',
					split: true,
					border: false,
					height: 40,
					collapseMode: 'mini',
					layout: 'fit',
					items: [detailPanel]
				}, {
					region: 'center',
					split: true,
					border: false,
					layout: 'fit',
					items: [userGrid]
				}]
			}]
		});
    })